package com.zhong.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.zhong.subject.application.convert.SubjectCategoryDTOConverter;
import com.zhong.subject.application.dto.SubjectCategoryDTO;
import com.zhong.subject.common.entity.Result;
import com.zhong.subject.domain.bo.SubjectCategoryBO;
import com.zhong.subject.domain.service.SubjectCategoryDomainService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 刷题分类Controller
 */
@Slf4j
@RestController
@RequestMapping("/subject/category")
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryDomainService subjectCategoryDomainService;

    /**
     * 增加题目分类
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            if(log.isInfoEnabled()){
                //此判断是为了增加日志打印的效率
                log.info("SubjectCategoryController.add.dto：{}", JSON.toJSONString(subjectCategoryDTO));
            }
            //数据校验
            Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(subjectCategoryDTO.getCategoryName()),"分类名称不能为空");
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"父类ID不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            subjectCategoryDomainService.add(subjectCategoryBO);
            return Result.ok(true);
        }catch (Exception e){
            log.error("SubjectCategoryController.add.error：{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 查询分类中大类
     */
    @GetMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(){
        try {
            if(log.isInfoEnabled()){
                //此判断是为了增加日志打印的效率
                log.info("SubjectCategoryController.queryPrimaryCategory：{}", "查询中！！！");
            }
            SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertBoListToDTOList(subjectCategoryBOList);
            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryPrimaryCategory.error：{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 查询分类中大类下的子类
     */
    @GetMapping("/queryCategoryByPrimary")
    public Result<SubjectCategoryDTO> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            if(log.isInfoEnabled()){
                //此判断是为了增加日志打印的效率
                log.info("SubjectCategoryController.queryCategoryByPrimary.subjectCategoryDTO：{}", JSON.toJSONString(subjectCategoryDTO));
            }

            //数据校验
            Preconditions.checkNotNull(subjectCategoryDTO.getParentId(),"分类的父类id不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            List<SubjectCategoryBO> subjectCategoryBOList = subjectCategoryDomainService.queryCategory(subjectCategoryBO);

            List<SubjectCategoryDTO> subjectCategoryDTOList = SubjectCategoryDTOConverter.INSTANCE.convertBoListToDTOList(subjectCategoryBOList);

            return Result.ok(subjectCategoryDTOList);
        }catch (Exception e){
            log.error("SubjectCategoryController.queryCategoryByPrimary1.error：{}",e.getMessage(),e);
            return Result.fail("查询失败");
        }
    }

    /**
     * 更新分类
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.subjectCategoryDTO：{}",JSON.toJSONString(subjectCategoryDTO));
            }

            //数据校验
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类Id不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.update(subjectCategoryBO);

            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.update.error：{}",e.getMessage(),e);
            return Result.fail("更新分类失败");
        }
    }

    /**
     * 删除分类
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.subjectCategoryDTO：{}",JSON.toJSONString(subjectCategoryDTO));
            }

            //数据校验
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类Id不能为空");

            SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            Boolean result = subjectCategoryDomainService.delete(subjectCategoryBO);

            return Result.ok(result);
        }catch (Exception e){
            log.error("SubjectCategoryController.delete.error：{}",e.getMessage(),e);
            return Result.fail("删除分类失败");
        }
    }
}
